//
//  MovieViewController.m
//  Movie
//
//  Created by tengyang on 15/7/17.
//  Copyright (c) 2015年 tengyang. All rights reserved.
//

#import "MovieViewController.h"
#import "movieModal.h"
#import "MovieCell.h"
#import "PosterView.h"

@interface MovieViewController ()
{
    BOOL isLeft;
}
@end

@implementation MovieViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"电影";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"电影";

    [self _createNavRight];
    [self _createMovieTableView];
    
    [self _createPosterView];
    [self _loadData];
    
    self.edgesForExtendedLayout=UIRectEdgeNone;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - loadData-Json

//使用解析json数据
- (void)_loadData
{
    NSString * string=[[NSBundle mainBundle] pathForResource:@"us_box.json" ofType:nil];
    NSData *data=[NSData dataWithContentsOfFile:string];
    //如果json数据的结构是字典类型的就用字典，如果是数组类型的就用数组
    NSDictionary *dataDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *subjects=[dataDic objectForKeyedSubscript:@"subjects"];
    _movieModalArray=[[NSMutableArray alloc]init];
    
    for (NSDictionary *dic in subjects) {
        NSDictionary *subject=[dic objectForKey:@"subject"];
        movieModal *modal=[[movieModal alloc]init];
        modal.title=[subject objectForKey:@"title"];
        modal.year=[subject objectForKey:@"year"];
        modal.images=[subject objectForKey:@"images"];
        NSNumber *number=[[subject objectForKey:@"rating"]objectForKey:@"average"];
        modal.average=number.floatValue;
        [_movieModalArray addObject:modal];
        //NSLog(@"%@",modal.images);
        //NSLog(@"%@",modal);
    }
    _posterView.movieModalArray=_movieModalArray;
    
    
    

}



#pragma mark - create
- (void)_createNavRight
{
    
    UIView *flipView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [button1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    button1.tag=1;
    [button1 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
    [button1 sizeToFit];
    [flipView addSubview:button1];
    
    UIButton *button2=[UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setImage:[UIImage imageNamed:@"list_home" ] forState:UIControlStateNormal];
    [button2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    button2.tag=2;
    [button2 sizeToFit];
    [button2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
    [flipView addSubview:button2];
    
    button2.hidden=YES;
    
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:flipView];
    self.navigationItem.rightBarButtonItem=item;
    
}

- (void)_createMovieTableView
{
    _moviewTableView =[[UITableView alloc]initWithFrame:self.view.bounds];
    _moviewTableView.dataSource=self;
    _moviewTableView.delegate=self;
    _moviewTableView.hidden=YES;
    [self.view addSubview:_moviewTableView];
    
    UINib *nib=[UINib nibWithNibName:@"MovieCell" bundle:[NSBundle mainBundle]];
    [_moviewTableView registerNib:nib forCellReuseIdentifier:@"cell"];
}

- (void)_createPosterView
{
    _posterView=[[PosterView alloc]initWithFrame:self.view.bounds];
    _posterView.backgroundColor=[UIColor grayColor];
    [self.view addSubview:_posterView];
    
}

#pragma mark - Action
- (void)flipAction:(UIButton *)button
{
    UIView *flip=self.navigationItem.rightBarButtonItem.customView;
    UIButton *button1=(UIButton *)[flip viewWithTag:1];
    UIButton *button2=(UIButton *)[flip viewWithTag:2];
    UIViewAnimationOptions option=button1.hidden?UIViewAnimationOptionTransitionFlipFromRight:UIViewAnimationOptionTransitionFlipFromLeft;
    //isLeft=!isLeft;
    
    //实现翻转效果，但只是效果而已，具体的还是要靠hidden的取反来实现
    [UIView transitionWithView:flip duration:0.5 options:option animations:nil completion:nil];
    [UIView transitionWithView:self.view duration:0.5 options:option animations:nil completion:nil];
    button1.hidden=!button1.hidden;
    button2.hidden=!button2.hidden;
    
    _moviewTableView.hidden=!_moviewTableView.hidden;
    _posterView.hidden=!_posterView.hidden;
}



#pragma mark - _movieTableView degate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _movieModalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MovieCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    movieModal *modal =_movieModalArray[indexPath.row];
    
    cell.modal=modal;
    [cell setBackgroundColor:[UIColor blackColor]];
    return cell;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}



@end
